//
//  QJShopCarBottomView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/7.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJShopCarBottomView.h"

@interface QJShopCarBottomView ()
@property (nonatomic, strong) UILabel *totalLabel; // 合计
@property (nonatomic, strong) UIButton *payBtn;  // 结算或者删除

@end

@implementation QJShopCarBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.editState = QJShopCarBottomViewStateNormal;
        self.selectState = QJShopCarBottomViewStateDisSelectAll;
        
        [self makeView:frame];
    }
    return self;
}

- (void)makeView:(CGRect)frame {
    
    // 结算
    CGFloat payBtnW = 160.f/750.f * UISCREEN_WIDTH;
    CGFloat payBtnX = frame.size.width - payBtnW;
    UIButton *payBtn = [[UIButton alloc] init]; // 160
    payBtn.frame = CGRectMake(payBtnX, 0, payBtnW, frame.size.height);
    [payBtn setTitle:@"结算" forState:UIControlStateNormal];
    [payBtn setTitle:@"删除" forState:UIControlStateSelected];
    payBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    payBtn.backgroundColor = YFColor_Red;
    [payBtn addTarget:self action:@selector(payBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:payBtn];
    self.payBtn = payBtn;
    
    // 全选
    UIButton *selectedAllBtn = [[UIButton alloc] init];
    selectedAllBtn.frame = CGRectMake(0, 0, 160.f/750.f * UISCREEN_WIDTH, frame.size.height);
    [selectedAllBtn setTitle:@"全选" forState:UIControlStateNormal];
    [selectedAllBtn setTitleColor:YFColor_Text forState:UIControlStateNormal];
    selectedAllBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [selectedAllBtn addTarget:self action:@selector(selectedAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [selectedAllBtn setImage:[UIImage imageNamed:@"Snoselect"] forState:UIControlStateNormal];
    [selectedAllBtn setImage:[UIImage imageNamed:@"Sselect"] forState:UIControlStateSelected];
    [self addSubview:selectedAllBtn];
    selectedAllBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    self.selectedAllBtn = selectedAllBtn;
    
    // 价格
    CGFloat priceW = 80;
    CGFloat priceX = frame.size.width - payBtnW - priceW;
    UILabel *priceLabel = [[UILabel alloc] init];
    priceLabel.frame = CGRectMake(priceX, 0, priceW, frame.size.height);
    priceLabel.text = @"¥1222.22";
    priceLabel.textColor = YFColor_Red;
    priceLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:priceLabel];
    self.priceLabel = priceLabel;
    
    // 合计
    CGFloat totalW = 50;
    CGFloat totalX = CGRectGetMinX(priceLabel.frame) - totalW;
    UILabel *totalLabel = [[UILabel alloc] init];
    totalLabel.frame = CGRectMake(totalX, 0, totalW, frame.size.height);
    totalLabel.text = @"合计:";
    totalLabel.font = [UIFont systemFontOfSize:16];
    totalLabel.textColor = YFColor_Text;
    [self addSubview:totalLabel];
    self.totalLabel = totalLabel;
    
    // line
    CGFloat lineH = 0.5;
    UIView *line = [[UIView alloc] init];
    line.frame = CGRectMake(0, 0, frame.size.width, lineH);
    line.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:line];
    
}

- (void)bottomViewUpdateDataWithPrice:(CGFloat)price{
    self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",price];
    
    // 重新计算价格宽度
    [YFGlobalTool sizeWithString:self.priceLabel.text font:self.priceLabel.font];
}

- (void)payBtnClick {
    if (self.payBtnClickBlock) {
        self.payBtnClickBlock();
    }
}

- (void)selectedAllBtnClick:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (btn.selected) {
        [dic setObject:@"1"forKey:@"selected"];
    }else{
        
         [dic setObject:@"0"forKey:@"selected"];
    }
  
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SelectAllOrDisSelecteAllNotification" object:dic];
}

- (void)setEditState:(QJShopCarBottomViewEditState)editState {
    _editState = editState;
    
    if (editState == QJShopCarBottomViewStateNormal) { // 一般状态
        self.totalLabel.hidden = NO;
        self.priceLabel.hidden = NO;
        self.payBtn.selected = NO;
    }else if (editState == QJShopCarBottomViewStateEdit) { // 编辑状态
        self.totalLabel.hidden = YES;
        self.priceLabel.hidden = YES;
        self.payBtn.selected = YES;
    }
}



@end
